package org.zfes.snowier.cloud.zbss.cms.column.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.column.service.ICmsColumnUserDefineAttrService;
import org.zfes.snowier.cloud.zbss.cms.column.dao.CmsColumnUserDefineAttrMapper;
import org.zfes.snowier.cloud.zbss.cms.column.model.CmsColumnUserDefineAttr;
import java.util.stream.Stream;
@Service
public class CmsColumnUserDefineAttrServiceImpl implements ICmsColumnUserDefineAttrService{
 	@Autowired
	private CmsColumnUserDefineAttrMapper cmsColumnUserDefineAttrMapper;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAndUpdateCmsColumnUserDefineAttr(Long columnId,Map<String,String> columnUserDefineAttrMap) {
		//栏目校验
		ZAssert.notNull("columnId", "栏目Id不能为空");
		
		CmsColumnUserDefineAttr userDefineAttr = new CmsColumnUserDefineAttr();
		ZBeanUtil.setCreateInfo(userDefineAttr);
		userDefineAttr.setColumnId(columnId);
		columnUserDefineAttrMap.forEach((attrKey,attrValue)->{
			Optional<CmsColumnUserDefineAttr> columnUserDefineAttrOps= cmsColumnUserDefineAttrMapper.selectByColumnIdAndAttrKey(columnId, attrKey);
			userDefineAttr.setAttrKey(attrKey);
			userDefineAttr.setAttrValue(attrValue);
			//不存在
			if(!columnUserDefineAttrOps.isPresent()) {
				cmsColumnUserDefineAttrMapper.insertSelective(userDefineAttr);
			}
			else {
				cmsColumnUserDefineAttrMapper.update(userDefineAttr);
			}
		});
 
	}
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsColumnUserDefineAttr(CmsColumnUserDefineAttr cmsColumnUserDefineAttr) {
		ZBeanUtil.setCreateInfo(cmsColumnUserDefineAttr);
		ZBeanUtil.validateBean(cmsColumnUserDefineAttr);
		cmsColumnUserDefineAttrMapper.insertSelective(cmsColumnUserDefineAttr);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsColumnUserDefineAttr> loadCmsColumnUserDefineAttrById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsColumnUserDefineAttrMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsColumnUserDefineAttr(CmsColumnUserDefineAttr cmsColumnUserDefineAttr) {
		ZBeanUtil.validateBean(cmsColumnUserDefineAttr);
		cmsColumnUserDefineAttr.setUpdateTime(ZDateUtil.newDate());
		cmsColumnUserDefineAttrMapper.updateByPrimaryKeySelective(cmsColumnUserDefineAttr);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsColumnUserDefineAttr(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsColumnUserDefineAttrMapper.updateStatus(id, Byte.valueOf("-1"));
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsColumnUserDefineAttrStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsColumnUserDefineAttrMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsColumnUserDefineAttrDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsColumnUserDefineAttrMapper.selectListCount(pm), cmsColumnUserDefineAttrMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsColumnUserDefineAttrStatusByColumnId(Long columnId, Byte status) {
		ZAssert.notNull(columnId, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsColumnUserDefineAttrMapper.updateStatusByColumnId(columnId, status); ;
		
	}
	
	
}